<?php
/**
 * Created by PhpStorm.
 * User: iceberg
 * Date: 2018-10-12
 * Time: 14:43
 */

namespace app\validate;


use think\Validate;

class UserValidate extends Validate
{
    protected $rule = [
        'username'      =>  'require|max:10|unique:user',
        'password'      =>  'require',
        'phone'         =>  'number',
        'email'         =>  'email',
        'truename'      =>  'max:10',
        'status'        =>  'require|number',
        'role'          =>  'require|number',
        'yzm'           =>  'require|captcha',
    ];

    protected $message  =   [
        'username.require'      =>  '用户名必填',
        'username.max'          =>  '用户名最多不能超过10个字符',
        'username.unique'       =>  '用户名已经存在',
        'password.require'      =>  '密码必填',
        'phone'                 =>  '电话号码格式错误',
        'email'                 =>  '电子邮箱格式错误',
        'truename.max'          =>  '真实用户最多不能超过10个字符',
        'status.require'        =>  '状态必填',
        'status.number'         =>  '状态必须为数字',
        'role.require'          =>  '权限必填',
        'role.number'           =>  '权限必须为数字',
        'yzm.require'           =>  '验证码必填',
        'yzm.captcha'           =>  '验证码错误',
    ];

    //场景
    protected $scene = [
        'add'       =>  ['username', 'phone', 'email', 'truename', 'status', 'role'],
        'edit'      =>  ['username', 'phone', 'email', 'truename', 'status', 'role'],
    ];

    //验证场景定义
    public function sceneLogin()
    {
        return $this->only(['username', 'password'])->remove('username', 'unique');
    }

    //带验证码的验证登录场景定义
    public function sceneLogin_yzm()
    {
        return $this->only(['username', 'password', 'yzm'])->remove('username', 'unique');
    }
}